#include "stm32f10x.h"                  // Device header

void PWM_TIM2_CH1_Init(void)
{
	//启用GPIOA0
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	/* 以下代码可将原来TIM2_CH1对应的PA0端口重映射至PA15
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
	GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);//将CH1重映射至PA15
	GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//针对PA15解除JTAG调试端口的复用，保留SWJ
	*/
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
	//GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2使能
	TIM_InternalClockConfig(TIM2); //选择内部时钟作为时钟源，可省略
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 100-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 720-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初值
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性 不翻转
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 10; //CCR
	TIM_OC1Init(TIM2,&TIM_OCInitStructure);
	
	TIM_Cmd(TIM2,ENABLE); //启动计时器
}

void PWM_TIM2_CH2_Init(void)
{
	//启用GPIOA1
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM2使能
	TIM_InternalClockConfig(TIM2); //选择内部时钟作为时钟源，可省略
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 100-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 720-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初值
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性 不翻转
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 10; //CCR
	TIM_OC2Init(TIM2,&TIM_OCInitStructure);
	
	TIM_Cmd(TIM2,ENABLE); //启动计时器
}

void PWM_TIM3_Init(void)
{
	//启用GPIOA6
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	TIM_InternalClockConfig(TIM3);
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
	TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //滤波采样分频
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数
	TIM_TimeBaseInitStructure.TIM_Period = 200-1; //ARR自动重装器的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 7200-1; //PSC预分频
	TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器，先不用管
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
	
	TIM_OCInitTypeDef TIM_OCInitStructure;
	TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初值
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //pwmMode1在向上计数时，ccr<cnt则ref为高
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //极性 不翻转，实际输出等于ref
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStructure.TIM_Pulse = 5; //CCR
	TIM_OC1Init(TIM3,&TIM_OCInitStructure); //TIM3_CH1对应PA6
	
	TIM_Cmd(TIM3,ENABLE); //启动计时器
}

void PWM_TIM2_SetCompare1(u16 Compare)
{
	TIM_SetCompare1(TIM2,Compare);
}

void PWM_TIM2_SetCompare2(u16 Compare)
{
	TIM_SetCompare2(TIM2,Compare);
}

void PWM_TIM3_SetCompare1(u16 Compare)
{
	TIM_SetCompare1(TIM3,Compare);
}

void PWM_TIM2_SetPrescaler(u16 prescaler)
{
	//参数3代表周期结束后生效而不是立即生效
	TIM_PrescalerConfig(TIM2,prescaler,TIM_PSCReloadMode_Update);
}
